package com.bbva.general.generics;

import java.io.Serializable;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpServletRequest;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.bbva.general.generics.ControllerHelper;
import com.bbva.general.generics.ServiceFinder;
import com.bbva.general.generics.ServiceGeneric;
@SuppressWarnings("unchecked")
public abstract class AbstractHibernateDAOImpl <T extends Serializable, KeyType extends Serializable> 
       extends HibernateDaoSupport {

	//private String idController;
	private String forwardPadre;

//	public String getIdController() {
//		return idController;
//	}
//
//	public void setIdController(String idController) {
//		this.idController = idController;
//	}

	public String getForwardPadre() {
		return forwardPadre;
	}

	public void setForwardPadre(String forwardPadre) {
		this.forwardPadre = forwardPadre;
	}

	protected Class<T> domainClass = getDomainClass();

	/**
	 * Method to return the class of the domain object
	 */
	protected abstract Class<T> getDomainClass();
	
	public T load(KeyType id) {
		return (T) ((HibernateTemplate)ServiceFinder.findBean("hibernateTemplate")).load(domainClass, id);
	}
	public T get(KeyType id) {
		return (T) ((HibernateTemplate)ServiceFinder.findBean("hibernateTemplate")).get(domainClass, id);
	}
	public void update(T t) {
		((HibernateTemplate)ServiceFinder.findBean("hibernateTemplate")).update(t);
	}

	public void save(T t) {
		((HibernateTemplate)ServiceFinder.findBean("hibernateTemplate")).save(t);
	}

	public void delete(T t) {
		((HibernateTemplate)ServiceFinder.findBean("hibernateTemplate")).delete(t);
	}
	
	public List<T> getList() {		
		return (((HibernateTemplate)ServiceFinder.findBean("hibernateTemplate")).find("from " + domainClass.getName()
				+ " x"));
	}
	
	public List<T> getList(String campo1, String campo2) {		
		return (((HibernateTemplate)ServiceFinder.findBean("hibernateTemplate")).find("from " + domainClass.getName()
				+ " x order by " + campo1 + " asc, " + campo2 + " asc"));
	}
	
	public List<T> getListFilter(String campo, char valor) {
		return (((HibernateTemplate)ServiceFinder.findBean("hibernateTemplate")).find("from " + domainClass.getName()+ " x where upper("+campo+") = upper('"+valor+"')"));
	}
	
	
	public List<T> getCustomList(String campo, Object valor) {
		return (((HibernateTemplate)ServiceFinder.findBean("hibernateTemplate")).find("from " + domainClass.getName()+ " x where "+campo+" = '"+String.valueOf(valor)+"'"));
	}
	
	public List<T> getCustomFilterList(String campo, Object valor, String campo2, char valor2) {
		return (((HibernateTemplate)ServiceFinder.findBean("hibernateTemplate")).find("from " + domainClass.getName()+ " x where "+campo+" = '"+String.valueOf(valor)+"' and upper("+campo2+") = upper('"+valor2+"')"));
	}
	
	public List<T> getCustomListByPattern(String campo, Object valor) {
		return (((HibernateTemplate)ServiceFinder.findBean("hibernateTemplate")).find("from " + domainClass.getName()+ " x where upper("+campo+") like upper('"+String.valueOf(valor)+"%')"));
	}
	
	public List<T> getCustomListByPatternFilter(String campo, Object valor, String campo2, char valor2) {
		return (((HibernateTemplate)ServiceFinder.findBean("hibernateTemplate")).find("from " + domainClass.getName()+ " x where upper("+campo+") like upper('"+String.valueOf(valor)+"%') and upper("+campo2+") = upper('"+valor2+"')"));
	}
	
	public void deleteById(KeyType id) {
		Object obj = load(id);
		((HibernateTemplate)ServiceFinder.findBean("hibernateTemplate")).delete(obj);
	}

	public void deleteAll() {
		((HibernateTemplate)ServiceFinder.findBean("hibernateTemplate")).execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException {
				String hqlDelete = "delete " + domainClass.getName();
				@SuppressWarnings("unused")
				int deletedEntities = session.createQuery(hqlDelete)
						.executeUpdate();
				return null;
			}

		});
	}

	public int count() {
		List list = ((HibernateTemplate)ServiceFinder.findBean("hibernateTemplate")).find(
				"select count(*) from " + domainClass.getName() + " x");
		Integer count = (Integer) list.get(0);
		return count.intValue();
	}
	
	public String eliminarRegistro()
	{
		return "";
	}
	
	/**
	 * Metodo para obtener la session del Request
	 * 
	 * @return
	 */
	protected HttpServletRequest getRequest() {
		return (HttpServletRequest) FacesContext.getCurrentInstance()
				.getExternalContext().getRequest();
	}
	
	public void verificarEliminacion(ActionEvent evt){
		ServiceGeneric serviceG = new ServiceGeneric();
		String idController = getRequest().getParameter("idController");
		String forwardPadre = getRequest().getParameter("forwardPadre");
		System.out.println("controller=" + idController);
		System.out.println("forwarpadre=" + forwardPadre);
		setForwardPadre(forwardPadre);
		ControllerHelper controllerHelper= (ControllerHelper)serviceG.getSpringBean("controllerHelper");
		controllerHelper.establecerController(idController);
	}
	
}
